/*************************************************************************
	> File Name: factorial.cpp
	> Author: 
	> Mail: 
	> Created Time: Sun 18 Aug 2024 08:04:00 AM CST
 ************************************************************************/

#include<iostream>
#include <stack>
using namespace std;

int f(int n) {
	if(n == 1) return 1;
	return n * f(n - 1);
}

struct Data {
	Data(int n, int *pre_ret) : n(n), pre_ret(pre_ret) {
		code = 0;
	}
	int n;
	int code, ret, *pre_ret;
} ;
int f1(int n) {
	stack<Data> s;
	int ans;
	Data d(n, &ans);
	s.push(d);
	while (!s.empty()) {
		Data &cur = s.top();
		switch (cur.code)
		{
		case 0: {
			if(cur.n == 1) {
				*(cur.pre_ret) = 1;
				s.pop();
			} else {
				cur.code = 1;
			}
			} break;
		case 1: {
			Data d(cur.n - 1, &(cur.ret));
			cur.code = 2;
			s.push(d);
			} break;
		case 2: {
			*(cur.pre_ret) = cur.ret * cur.n;
			s.pop();
			} break;
		}
	}
	return ans;
}
int main() {
	int n;
	while(cin >> n) {
		cout << "递归 : " << f(n) << endl;
		cout << "非递归 : " << f1(n) << endl;
	}
	return 0;
}
